/*
 * Created on 04/08/2010
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package graphic;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Turtle extends JPanel{

	Component filho;
	Graphics g = null;
	boolean traca = true;
	int w, h;
	int x = 0, y = 0;
	int anglo = 0;
	Component pai = null;
	Vector commands = new Vector();
	int parameters[] = new int[4];
	
	
	
	public Turtle(Component pai, Component filho, int wi, int he) {
		this.filho = filho; //g = filho.getGraphics();
		w = wi;
		h = he;
		x = w / 2;
		y = h / 2;
		this.pai = pai;
	}
	
	private void setParameters(int a, int b, int x, int y)
	{
		
		parameters[0] = a;	
		parameters[1] = b;
		parameters[2] = x;
		parameters[3] = y;
		//System.out.println(" a "+ a+"\n b "+b+"\n x "+x+"\n y "+y);
		
	}
	
	public void up() {
		traca = false;
	}

	public void down() {
		traca = true;
	}

	public void move(int r) {
		int dx, dy;
		double f = toRad(anglo);
		dx = (int) (r * Math.cos(f));
		dy = (int) (r * Math.sin(f));
		int a = x, b = y;
		x += dx;
		y += dy;
		if ( traca ) 
		{ 
		filho.getGraphics().drawLine(a, b, x, y);
		parameters = new int[4];
		setParameters(a, b, x, y);
		commands.add(parameters);
		}
	}

	private double toRad(int anglo2) {
		return anglo * Math.PI / 180.0;
	}

	public void go(int i, int j) {
		int a = x, b = y;
		x = w / 2 + i;
		y = h / 2 + j;
		if ( traca ) 
		{
		filho.getGraphics().drawLine(a, b, x, y);
		setParameters(a, b, x, y);
		commands.add(parameters);
		}	
	}
	
	public Vector getCommands()
	{
		return commands;
	}
	

	public void setAngle(int i) {
		i = i % 360;
		if ( i < 0 )
			i = 360 + i;
		anglo = i;
	}

	public int getAngle() {
		return anglo;
	}

	public int ask() {
		int k = 0;
		boolean cont = true;
		do {
			String s = JOptionPane.showInputDialog(pai, "Digite o valor");
			try {
				k = Integer.parseInt(s);
				cont = false;
			}
			catch (Exception e)
			{
				;
			}
			
		} while (cont);
		
		return k;
	}
	
	
}
